package cn.jinjimi.app.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.ZdbProduct;
import cn.jinjimi.app.main.adapter.extend.MyBaseAdapter;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;
import cn.jinjimi.app.main.widget.ZdbFeeTextView;

/**
 *
 * @author zhenhua
 */
public class ProductAdapter extends MyBaseAdapter<ZdbProduct> {
    private Context mContext;
    private boolean isShowMode;
    private String keyword;
    private String[] keywords;

    public ProductAdapter(Context ctx) {
        mContext = ctx;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_home_zdb_pro, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ZdbProduct product = getItem(position);

        holder.tvTitle.setText("");
        holder.tvDescription.setText(product.descr);
        holder.tvFee.setText(product.priceTitle);
        holder.tvDesc.setZdbFeeText(product.commissionDesc);
        holder.ivLogo.loadView(product.iconUrl);

        holder.tvDesc.setVisibility(isShowMode && AccountManager.getInstance().isLogin() ? View.INVISIBLE : View.VISIBLE);

        if (!StringUtils.isEmpty(product.salesTag)) {
            holder.tvHotTips.setVisibility(View.VISIBLE);
            holder.tvHotTips.setText(product.salesTag);
        } else {
            holder.tvHotTips.setVisibility(View.GONE);
        }

        return convertView;
    }

    private class ViewHolder {
        public TextView tvTitle, tvDescription, tvFee, tvTag, tvHotTips;
        public ZdbFeeTextView tvDesc;
        public FrescoImageView ivLogo;

        public ViewHolder(View v) {
            tvTitle = (TextView) v.findViewById(R.id.tv_title);
            tvDescription = (TextView) v.findViewById(R.id.tv_description);
            tvFee = (TextView) v.findViewById(R.id.tv_price);
            tvDesc = (ZdbFeeTextView) v.findViewById(R.id.tv_desc);
            tvTag = (TextView) v.findViewById(R.id.tv_type);
            ivLogo = (FrescoImageView) v.findViewById(R.id.iv_logo);
            tvHotTips = (TextView) v.findViewById(R.id.tv_hot_tips);
        }
    }

    public void setShowMode(boolean showMode) {
        isShowMode = showMode;
        notifyDataSetChanged();
    }


    public void setKeyword(String keyword) {
        this.keyword = keyword;
        keywords = keyword == null ? null : keyword.split(" ");
    }
}
